package com.kbbywyclient.ui.fragment.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.kbbywyclient.app.App;
import com.kbbywyclient.data.entity.User;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.util.pre.PreUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by FHW
 * on 2017/3/28
 */

public abstract class BaseFragment extends Fragment {
    private CompositeSubscription mCompositeSubscription;

    ProgressDialog progressDialog;//进度框

    private Tracker mTracker;

    protected abstract int provideContentViewId();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(provideContentViewId(), container, false);
        ButterKnife.bind(this, rootView);
        initView();
        initAppComponent(App.getInstance().getAppComponent());
        progressDialog = new ProgressDialog(getContext());
//        initData();
        return rootView;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    public void setScreenName(String s) {
        mTracker = App.getInstance().getDefaultTracker();
        mTracker.setScreenName(s);
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }

    public void setScreenAction(String category, String action) {
        mTracker = App.getInstance().getDefaultTracker();
        mTracker.send(new HitBuilders.EventBuilder()
                .setCategory(category)
                .setAction(action)
                .build());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getContext());
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getContext());
    }

    /**
     * add subscription, unsubscribe it when activity ondestory
     *
     * @param subscription
     */
    public void addSubscription(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(subscription);
    }

    protected abstract void initAppComponent(AppComponent appComponent);

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }


    public void showProgress() {
        showProgress("", "");
    }

    public void showProgress(String content) {
        showProgress("", content);
    }

    public void showProgress(int contentRes) {
        showProgress("", getString(contentRes));
    }

    /**
     * 显示进度框
     *
     * @param title
     * @param content
     */
    public void showProgress(String title, String content) {
        if (progressDialog != null)
            if (progressDialog.isShowing())
                progressDialog.dismiss();
        progressDialog.setTitle(title);
        progressDialog.setMessage(content);
        progressDialog.show();
    }

    /**
     * 隐藏进度框
     */
    public void dismissProgress() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    /**
     * 获取用户信息
     * @return
     */
    public User getUser() {
        Context context=getContext();
        if(context==null){
            context=App.getInstance();
        }
        return PreUtils.getUser(context);
    }
}
